﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using prohomework.Models;
using System.Data.SqlClient;
using System.Data;
using prohomework.Models.SqlClient;
using System.Web.Mvc;
namespace prohomework.Controllers
{
    public class LearnResourseBL:SqlBaseDBHandler<LearnResource>
    {
        private const string _getModel = "select * from Video Where ID=@ID";
        private const string _UpdataModel = "Update Video Set count=isnull(count,0)+1 Where  ID=@ID";
        private const string _GetVideoTypeModel = "select * from Video_Type order by ID Asc";
        public LearnResourseBL() : base(DBConnection.connectionstring)
        {
            CmdType = CommandType.Text;

        }

        protected override void AddParametersFromModel(LearnResource model)
        {
                  Parameters.AddWithValue("@ID", model.LR_ID);
            //       parameters.addwithvalue("@schoolname", model.schoolname);
        }

        protected override LearnResource AssembleFromReader(SqlDataReader reader)
        {
            if (reader.Read())
            {
                LearnResource sm = new LearnResource();
                    sm.LR_ID = reader["ID"].ToString();
                   sm.LR_Video_Name = reader["Video_Name"].ToString();
                sm.LR_Video_Url= reader["Video_Url"].ToString();
                sm.Addtime = reader["AddTime"]==System.DBNull.Value? " ": Convert.ToDateTime(reader["AddTime"]).ToShortDateString();
                return sm;           
            }
            else
                return null;
        }

        protected  VideoType AssembleFromReader_VideoType(SqlDataReader reader)
        {
            if (reader.Read())
            {
                VideoType sm = new VideoType();
                sm.VT_ID = reader["ID"].ToString();
                sm.VT_VideoType_Name = reader["Video_Type"].ToString();
               
                return sm;
            }
            else
                return null;
        }



        public override int Create(LearnResource dbobj)
        {
            CommandText = "sdfsd";
            AddParametersFromModel(dbobj);
            int result;
            return Executant.Act(out result);
        }

        public override bool Update(LearnResource dbobj)
        {
                CommandText = _UpdataModel;
            AddParametersFromModel(dbobj);
            int result;
            Executant.Act(out result);
            return result >= 1;
        }

        public override bool Delete(LearnResource dbobj)
        {
            //      CommandText = sp_school_Delete;
            //  Parameters.AddWithValue("@ID", dbobj.ID);
            int result;
            Executant.Act(out result);
            return result >= 1;
        }

        protected override LearnResource GetModel(int id)
        {
            CommandText = _getModel;
            Parameters.AddWithValue("@ID", id);
            SqlDataReader reader;
            Executant.Act(out reader);
            LearnResource model = AssembleFromReader(reader);
            CloseReader(reader);
            return model;
        }


        public List<LearnResource> GetAll(string sql)
        {
            CommandText = sql;
            SqlDataReader reader;
            this.Executant.Act(out reader);
            List<LearnResource> models = new List<LearnResource>();
            LearnResource model;
            //while (reader.Read())
            //{
            //    SelectListItem sm = new SelectListItem();
            //    sm.Value = reader[value].ToString();
            //    sm.Text = reader[text].ToString();
            //    models.Add(sm);
            //}
            while (true)
            {
                model = AssembleFromReader(reader);
                if (model != null)
                    models.Add(model);
                else
                    break;
            }
            this.CloseReader(reader);
            return models;
        }


        public List<VideoType> GetAllVideoType()
        {
            CommandText = _GetVideoTypeModel;
            SqlDataReader reader;
            this.Executant.Act(out reader);
            List<VideoType> models = new List<VideoType>();
            VideoType model;
            //while (reader.Read())
            //{
            //    SelectListItem sm = new SelectListItem();
            //    sm.Value = reader[value].ToString();
            //    sm.Text = reader[text].ToString();
            //    models.Add(sm);
            //}
            while (true)
            {
                model = AssembleFromReader_VideoType(reader);
                if (model != null)
                    models.Add(model);
                else
                    break;
            }
            this.CloseReader(reader);
            return models;
        }

        public LearnResource GetVideoModel(int id)
        {

            return GetModel(id);
        }






    }





    //文章操作
    public class ArticleBL : SqlBaseDBHandler<Article>
    {

        private const string _getModel = "Select * from ArticleView Where ArticleID=@ID";
        private const string _UpdataModel = "Update Article Set hits=isnull(hits,0)+1 Where  ArticleID=@ID";
        private const string _getClassname = "Select * from ArticleClass where ClassID=@ClassID";

        public ArticleBL() : base(DBConnection.connectionstring)
        {
            CmdType = CommandType.Text;

        }

        protected override void AddParametersFromModel(Article model)
        {
                  Parameters.AddWithValue("@ID", model.AT_ID);
            //       parameters.addwithvalue("@schoolname", model.schoolname);
        }
//文章详细页用
        protected override Article AssembleFromReader(SqlDataReader reader)
        {
            if (reader.Read())
            {
                Article sm = new Article();
                sm.AT_ID = reader["ArticleID"].ToString();
                sm.AT_Title = reader["Title"].ToString();
                sm.AT_AuthorName = reader["AuthorName"].ToString();
                sm.AT_updatetime= reader["updatetime"].ToString();
                sm.AT_hits = reader["hits"].ToString();
                sm.AT_ClassName = reader["ClassName"].ToString();
                sm.AT_Content = reader["Content"].ToString();
                sm.AT_pic = (reader["pic"]==System.DBNull.Value?null:(byte[])reader["pic"]);
                sm.AT_fileContentType = reader["fileContentType"].ToString();
                sm.AT_fileName = reader["fileName"].ToString();
                return sm;
            }
            else
                return null;
        }
// 首页显示前5条
        protected  Article AssembleFromReaderIndex(SqlDataReader reader)
        {
            if (reader.Read())
            {
                Article sm = new Article();
                sm.AT_ID = reader["ArticleID"].ToString();
                sm.AT_Title = reader["Title"].ToString();
             //   sm.AT_AuthorName= reader["AuthorName"].ToString();
           //     sm.AT_updatetime = reader["updatetime"].ToString();
                return sm;
            }
            else
                return null;
        }
//显示所有文章列表用
        protected Article AssembleFromReaderArticleList(SqlDataReader reader)
        {
     
            if (reader.Read())
            {
                Article sm = new Article();
                sm.AT_ID = reader["ArticleID"].ToString();
                sm.AT_Title = reader["Title"].ToString();
                   sm.AT_AuthorName= reader["AuthorName"].ToString();
                    sm.AT_updatetime = reader["updatetime"].ToString();
                sm.AT_hits = reader["hits"].ToString();
                return sm;
            }
            else
                return null;
        }


        public override int Create(Article dbobj)
        {
            CommandText = "sdfsd";
            AddParametersFromModel(dbobj);
            int result;
            return Executant.Act(out result);
        }

        public override bool Update(Article dbobj)
        {
               CommandText = _UpdataModel;
            AddParametersFromModel(dbobj);
            int result;
            Executant.Act(out result);
            return result >= 1;
        }

        public override bool Delete(Article dbobj)
        {
            //      CommandText = sp_school_Delete;
            //  Parameters.AddWithValue("@ID", dbobj.ID);
            int result;
            Executant.Act(out result);
            return result >= 1;
        }

        protected override Article GetModel(int id)
        {
            CommandText = _getModel;
            Parameters.AddWithValue("@ID", id);
            SqlDataReader reader;
            Executant.Act(out reader);
            Article model = AssembleFromReader(reader);
            CloseReader(reader);
            return model;
        }

        //protected  Article GetModelIndex(int id)
        //{
        //    CommandText = _getModel;
        //    Parameters.AddWithValue("@ID", id);
        //    SqlDataReader reader;
        //    Executant.Act(out reader);
        //    Article model = AssembleFromReaderIndex(reader);
        //    CloseReader(reader);
        //    return model;
        //}
//首页读取前五条
        public List<Article> GetAll(string sql)
        {
            CommandText = sql;
            SqlDataReader reader;
            this.Executant.Act(out reader);
            List<Article> models = new List<Article>();
            Article model;
            while (true)
            {
                model = AssembleFromReaderIndex(reader);
                if (model != null)
                    models.Add(model);
                else
                    break;
            }
            this.CloseReader(reader);
            return models;
        }
//读取所有文章列表
        public List<Article> GetAllList(string sql)
        {
            CommandText = sql;
            SqlDataReader reader;
            this.Executant.Act(out reader);
            List<Article> models = new List<Article>();
            Article model;
            int i = 1;
            while (true)
            {
                model = AssembleFromReaderArticleList(reader);
                if (model != null)
                {
                    model.xuhao = i.ToString();
                    models.Add(model);
                    i++;
                }
                else
                    break;
            }
            this.CloseReader(reader);
            return models;
        }
        //public Article GetFileModelIndex(int id)
        //{

        //    return GetModelIndex(id);
        //}

        public Article GetFileModel(int id)
        {

            return GetModel(id);
        }
//获取文章类别名称
        public   ArticleClass GetArticleClassModel(int id)
        {
            CommandText = _getClassname;
            Parameters.AddWithValue("@ClassID", id);
            SqlDataReader reader;
            Executant.Act(out reader);
            ArticleClass model = new ArticleClass();

            if (reader.Read())
            {
               // model.AC_ID = reader["ArticleID"].ToString();
                model.AC_Name = reader["ClassName"].ToString();
                //   sm.AT_AuthorName= reader["AuthorName"].ToString();
                //     sm.AT_updatetime = reader["updatetime"].ToString();
            //    return sm;
            }
            CloseReader(reader);
            return model;
        }


    }

    public class DownLoadFileBL : SqlBaseDBHandler<DownLoadFile>
    {
        private const string _getModel = "Select * from downfile where id=@ID";
        private const string _UpdataModel = "Update downfile Set downNum = isnull(downNum,0) + 1 where id=@ID";

        public DownLoadFileBL() : base(DBConnection.connectionstring)
        {
            CmdType = CommandType.Text;

        }

        protected override void AddParametersFromModel(DownLoadFile model)
        {
                 Parameters.AddWithValue("@ID", model.DLF_ID);
            //       parameters.addwithvalue("@schoolname", model.schoolname);
        }

        protected override DownLoadFile AssembleFromReader(SqlDataReader reader)
        {
            if (reader.Read())
            {
                DownLoadFile sm = new DownLoadFile();
                sm.DLF_ID = reader["ID"].ToString();
                sm.DLF_Title = reader["detail"].ToString();
                sm.DLF_FileName = reader["filename"].ToString();
                return sm;
            }
            else
                return null;
        }

        public override int Create(DownLoadFile dbobj)
        {
            CommandText = "sdfsd";
            AddParametersFromModel(dbobj);
            int result;
            return Executant.Act(out result);
        }

        public override bool Update(DownLoadFile dbobj)
        {
               CommandText = _UpdataModel;
               AddParametersFromModel(dbobj);
            int result;
            Executant.Act(out result);
            return result >= 1;
        }

        public override bool Delete(DownLoadFile dbobj)
        {
            //      CommandText = sp_school_Delete;
            //  Parameters.AddWithValue("@ID", dbobj.ID);
            int result;
            Executant.Act(out result);
            return result >= 1;
        }

        protected override DownLoadFile GetModel(int id)
        {
            CommandText = _getModel;
            Parameters.AddWithValue("@ID", id);
            SqlDataReader reader;
            Executant.Act(out reader);
            DownLoadFile model = AssembleFromReader(reader);
            CloseReader(reader);
            return model;
        }


        public List<DownLoadFile> GetAll(string sql)
        {
            CommandText = sql;
            SqlDataReader reader;
            this.Executant.Act(out reader);
            List<DownLoadFile> models = new List<DownLoadFile>();
            DownLoadFile model;
            while (true)
            {
                model = AssembleFromReader(reader);
                if (model != null)
                    models.Add(model);
                else
                    break;
            }
            this.CloseReader(reader);
            return models;
        }

        //public  bool Update(string sql)
        //{
        //    CommandText = sql;
        // //   AddParametersFromModel(dbobj);
        //    int result;
        //    Executant.Act(out result);
        //    return result >= 1;
        //}


        public DownLoadFile GetFileModel(int id)
        {

            return GetModel(id);
        }






    }
}